package com.flexpoker.server;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.flexpoker.client.PlayerService;
import com.flexpoker.domain.PMF;
import com.flexpoker.domain.Player;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class PlayerServiceImpl extends RemoteServiceServlet implements
		PlayerService {

	@Override
	public void createPlayer(Player player) throws IllegalArgumentException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(player);
		} finally {
			pm.close();
		}
	}

	@Override
	public Player getPlayerById(Long id) throws IllegalArgumentException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			return (Player) pm.getObjectById(id);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Player> getAllPlayers() throws IllegalArgumentException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Query query = pm.newQuery("select from Player");
			return (List<Player>) query.execute();
		} finally {
			pm.close();
		}
	}

}
